package com.linkage.usercenter.controller;


import cn.hutool.json.JSONUtil;
import com.github.pagehelper.PageInfo;
import com.linkage.common.base.RequestInfo;
import com.linkage.common.base.ResponseInfo;
import com.linkage.usercenter.pojo.entity.CoreNotice;
import com.linkage.usercenter.service.INoticeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 通知表 前端控制器
 * </p>
 *
 * @author shibiao
 * @since 2020-06-08
 */
@RestController
@RequestMapping("/notice")
@Slf4j
public class NoticeController {

    @Autowired
    private INoticeService iNoticeService;

    /**
     * 创建通知公告
     * @param coreNoticeRequestInfo
     * @return
     */
    @RequestMapping(value = "/createNotice", method = RequestMethod.POST)
    public String createNotice(@RequestBody RequestInfo<CoreNotice> coreNoticeRequestInfo){
        try {
            this.iNoticeService.createNotice(coreNoticeRequestInfo.getBuss());
            return JSONUtil.toJsonStr(ResponseInfo.success());
        } catch (Exception e) {
            log.error("",e);
            return JSONUtil.toJsonStr(ResponseInfo.error(e.getMessage()));
        }
    }

    /**
     * 通知公告详情
     * @param requestInfo
     * @return
     */
    @RequestMapping(value = "/singleNoticeShow", method = RequestMethod.POST)
    public String singleNoticeShow(@RequestBody RequestInfo<Map> requestInfo){
        try {
            CoreNotice coreNotice = this.iNoticeService.singleNoticeShow(requestInfo.getBuss().get("noticeId").toString());
            return JSONUtil.toJsonStr(ResponseInfo.success(coreNotice));
        } catch (Exception e) {
            log.error("",e);
            return JSONUtil.toJsonStr(ResponseInfo.error(e.getMessage()));
        }
    }


    /**
     * 通知公告列表查询
     * @param requestInfo
     * @return
     */
    @RequestMapping(value = "/listNoticeShow", method = RequestMethod.POST)
    public String listNoticeShow(@RequestBody RequestInfo<Map> requestInfo){
        try {
            Integer pageNo = Integer.parseInt(requestInfo.getBase().getPageNum());
            Integer pageSize = Integer.parseInt(requestInfo.getBase().getPageRows());
            String title = requestInfo.getBuss().containsKey("title")?String.valueOf(requestInfo.getBuss().get("title")):null;
            Integer type = requestInfo.getBuss().containsKey("type")?(Integer)(requestInfo.getBuss().get("type")):null;
            PageInfo<CoreNotice> pageInfo =  this.iNoticeService.listNoticeShow(pageNo, pageSize, title, type);
            return JSONUtil.toJsonStr(ResponseInfo.successWithPage(pageInfo));
        } catch (Exception e) {
            log.error("",e);
            return JSONUtil.toJsonStr(ResponseInfo.error(e.getMessage()));
        }
    }

    /**
     * 通知公告查询
     */
    @RequestMapping("/getList")
    public String getList(){
        List<CoreNotice> list = iNoticeService.queryList(new CoreNotice());
        return JSONUtil.toJsonStr(ResponseInfo.success(list));
    }

    @RequestMapping(value = "/deleteNotice", method = RequestMethod.POST)
    public String deleteNotice(@RequestBody RequestInfo<Map> requestInfo){
        try {
            this.iNoticeService.deleteNotice(requestInfo.getBuss().get("noticeId").toString());
            return JSONUtil.toJsonStr(ResponseInfo.success());
        } catch (Exception e) {
            log.error("",e);
            return JSONUtil.toJsonStr(ResponseInfo.error(e.getMessage()));
        }
    }

    @RequestMapping(value = "/updateNotice", method = RequestMethod.POST)
    public String updateNotice(@RequestBody RequestInfo<CoreNotice> cnRequestInfo){
        try {
            this.iNoticeService.updateNotice(cnRequestInfo.getBuss());
            return JSONUtil.toJsonStr(ResponseInfo.success());
        } catch (Exception e) {
            log.error("",e);
            return JSONUtil.toJsonStr(ResponseInfo.error(e.getMessage()));
        }
    }

}

